

class Channel
{
public:
    Channel(int pfd,pid_t id):_pfd(pfd),_id(id)
    {
        _name = "子进程：" + std::to_string(id);
    }
    std::string name()
    {
        return _name;
    }
    void close()
    {
        int n = ::close(_pfd);
        if(n == 0){}
        //std::cout << "关闭成功,pfd:" << _pfd << std::endl;
        else 
        std::cout << "关闭失败" << std::endl;
    }
    void send(int task)
    {
        // 派发任务
        ssize_t n = ::write(_pfd, &task, sizeof(task));
        if (n > 0) // 写入成功
        {
            // std::cout << "派发任务成功" << std::endl;
        }
        else if (n == 0)
        {
            std::cout << "子进程读端退出，me too" << std::endl;
        }
        else
        {
            std::cout << "读错误" << std::endl;
            exit(WRITERR);
        }
    }
    pid_t Getpid()
    {
        return _id;
    }
    ~Channel()
    {}
private:
    std::string _name;
    int _pfd;
    pid_t _id;
};